import { ref } from "vue";
import axios from "@/utils/axios";
import moment from "moment";

export default () => {
    const detailInfo: any = ref({});
    const queryinfo: any = ref({
        mpId: -1,
        mpName: "--",
        page: 1,
        pageSize: 50,
        startTime: null,
        endTime: null,
        total: null,
        dataType: 1,
        type: 1,
        sign: null,
    })
    const rawInfoList = ref(new Array());

    const showModalInfo: any = ref({
        isShow: false,
        isStableShow: false,
        data: {}
    })

    /**
     * 获取生产数据
     * @param mpId 拌合站id
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param pageSize 每页内容数量
     * @param page 当前页数
     */
    let getRawInfo = (mpId: number, startTime: string, endTime: string, sign: any, pageSize: number, page: number) => {
        if (typeof startTime == 'object') startTime = moment(startTime).format("YYYY-MM-DD") + " 00:00:00";
        if (typeof endTime == 'object') endTime = moment(endTime).format("YYYY-MM-DD") + " 23:59:59";
        let url = queryinfo.value.dataType == 1 ? "/mix/data/getDataByMpId":"/mix/data/getWarnDataByMpId"
        axios
            .post(url, {
                mpId: mpId,
                startTime: startTime,
                endTime: endTime,
                sign: sign,
                pageSize: pageSize,
                page: page,
            })
            .then((res)=>{
                if (page == null) {
                    queryinfo.value.page = 1;
                    queryinfo.value.pageSize = res.data.size;
                    queryinfo.value.total = res.data.total;

                    queryinfo.value.mpId = mpId;
                    queryinfo.value.startTime = startTime;
                    queryinfo.value.endTime = endTime;

                    queryinfo.value.sign = null
                }
                rawInfoList.value = res.data.data;
                if (queryinfo.value.type == 3) {
                    rawInfoList.value.forEach((item:any) => {
                        item.endTime = item.endTime.substring(0, 10)
                    })
                }
                console.log("*************************")
                console.log(rawInfoList.value)
            });
    }

    let getRawByLatery = () => {
        axios
            .post("/mix/data/getLatery")
            .then((res) => {
                rawInfoList.value = res.data;
            });
    }

    let getRawByLateryByMpId = (id: number | undefined) => {
        axios
            .post("/mix/data/getLateryByMpId", {
                id: id
            })
            .then((res) => {
                rawInfoList.value = res.data;
            });
    }

    const setDescribe = (type: number) => {
        let describe = "";
        switch (type) {
            case 1: describe = "配比异常"; break;
            case 2: describe = "粗骨料的施工用量与设计规范不符"; break;
            case 3: describe = "细骨料的施工用量与设计规范不符"; break;
            case 4: describe = "水泥的施工用量与设计规范不符"; break;
            case 5: describe = "水的施工用量与设计规范不符"; break;

            case 6: describe = "粗骨料的实际用量与施工用量不符"; break;
            case 7: describe = "粗骨料的实际用量与施工用量不符"; break;
            case 8: describe = "水泥的实际用量与施工用量不符"; break;
            case 9: describe = "水的实际用量与施工用量不符"; break;

            default: describe = "";
        }
        return describe;
    }

    /**
     * 获取详细信息
     */
    let getDetailInfo = (data: any) => {
        detailInfo.value = {};
        detailInfo.value.pfName = '';
        detailInfo.value.table = [];
        detailInfo.value.eachone = [];
        if (data.formal) {
            detailInfo.value.pfName=data.formal.name
         }
        
        if (data.formal) detailInfo.value.table.push({
            index: 1,
            cell0: "材料名称",
            cell1: data.formal.gl1,
            cell2: data.formal.gl2,
            cell3: data.formal.gl3,
            cell4: data.formal.gl4,
            cell5: data.formal.gl5,
            cell6: data.formal.gl6,
            cell7: data.formal.cement,
            cell8: data.formal.water,
            cell9: data.formal.additive,
            cell10: data.formal.flyash,
            cell11: "",
            cell12: ""
        })
        if (data.formal) detailInfo.value.table.push({
            index: 2,
            cell0: "设计配比(kg/m³)",
            cell1: data.formal.gl1Design || "",
            cell2: data.formal.gl2Design || "",
            cell3: data.formal.gl3Design || "",
            cell4: data.formal.gl4Design || "",
            cell5: data.formal.gl5Design || "",
            cell6: data.formal.gl6Design || "",
            cell7: data.formal.cementDesign || "",
            cell8: data.formal.waterDesign || "",
            cell9: data.formal.additiveDesign || "",
            cell10: data.formal.flyashDesign || "",
            cell11: "",
            cell12: ""
        })
        if (data.moisture) detailInfo.value.table.push({
            index: 3,
            cell0: "含水率(%)",
            cell1: data.moisture.gl1 ? data.moisture.gl1 + "%" : "",
            cell2: data.moisture.gl2 ? data.moisture.gl2 + "%" : "",
            cell3: data.moisture.gl3 ? data.moisture.gl3 + "%" : "",
            cell4: data.moisture.gl4 ? data.moisture.gl4 + "%" : "",
            cell5: data.moisture.gl5 ? data.moisture.gl5 + "%" : "",
            cell6: data.moisture.gl6 ? data.moisture.gl6 + "%" : "",
            cell7: "",
            cell8: data.moisture.waterPlan,
            cell9: "",
            cell10: "",
            cell11: "",
            cell12: ""
        })
        detailInfo.value.table.push({
            index: 4,
            cell0: "施工用量(kg)",
            cell1: data.gl1Plan ? data.gl1Plan : "",
            cell2: data.gl2Plan ? data.gl2Plan : "",
            cell3: data.gl3Plan ? data.gl3Plan : "",
            cell4: data.gl4Plan ? data.gl4Plan : "",
            cell5: data.gl5Plan ? data.gl5Plan : "",
            cell6: data.gl6Plan ? data.gl6Plan : "",
            cell7: data.cementPlan ? data.cementPlan : "",
            cell8: data.waterPlan ? data.waterPlan : "",
            cell9: data.additivePlan ? data.additivePlan : "",
            cell10: data.flyashPlan ? data.flyashPlan : "",
            cell11: data.yield ? data.yield : "",
            cell12: moment(data.endTime).diff(moment(data.startTime), 'seconds') + "s"
        })
        detailInfo.value.table.push({
            index: 5,
            cell0: "实际用量(kg)",
            cell1: data.gl1 ? data.gl1 : "",
            cell2: data.gl2 ? data.gl2 : "",
            cell3: data.gl3 ? data.gl3 : "",
            cell4: data.gl4 ? data.gl4 : "",
            cell5: data.gl5 ? data.gl5 : "",
            cell6: data.gl6 ? data.gl6 : "",
            cell7: data.cement ? data.cement : "",
            cell8: data.water ? data.water : "",
            cell9: data.additive ? data.additive : "",
            cell10: data.flyash ? data.flyash : "",
            cell11: data.yield ? data.yield : "",
            cell12: moment(data.endTime).diff(moment(data.startTime), 'seconds') + "s"
        })
        if (data.formal) detailInfo.value.table.push({
            index: 6,
            cell0: "偏差率(%)",
            cell1: data.gl1Plan ? Math.abs((data.gl1 - data.gl1Plan) / data.gl1Plan * 100).toFixed(2) + "%" : "",
            cell2: data.gl2Plan ? Math.abs((data.gl2 - data.gl2Plan) / data.gl2Plan * 100).toFixed(2) + "%" : "",
            cell3: data.gl3Plan ? Math.abs((data.gl3 - data.gl3Plan) / data.gl3Plan * 100).toFixed(2) + "%" : "",
            cell4: data.gl4Plan ? Math.abs((data.gl4 - data.gl4Plan) / data.gl4Plan * 100).toFixed(2) + "%" : "",
            cell5: data.gl5Plan ? Math.abs((data.gl5 - data.gl5Plan) / data.gl5Plan * 100).toFixed(2) + "%" : "",
            cell6: data.gl6Plan ? Math.abs((data.gl6 - data.gl6Plan) / data.gl6Plan * 100).toFixed(2) + "%" : "",
            cell7: data.cementPlan ? Math.abs((data.cement - data.cementPlan) / data.cementPlan * 100).toFixed(2) + "%" : "",
            cell8: data.waterPlan ? Math.abs((data.water - data.waterPlan) / data.waterPlan * 100).toFixed(2) + "%" : "",
            cell9: data.additivePlan ? Math.abs((data.additive - data.additivePlan) / data.additivePlan * 100).toFixed(2) + "%" : "",
            cell10: data.flyashPlan ? Math.abs((data.flyash - data.flyashPlan) / data.flyashPlan * 100).toFixed(2) + "%" : "",
            cell11: "",
            cell12: ""
        })
        if (data.formal) detailInfo.value.table.push({
            index: 7,
            cell0: "控制标准(%)",
            cell1:data.formal.gl1Type == 1 ?(data.formal.coarseAggregatePlan?data.formal.coarseAggregatePlan+ "%" : "") : (data.formal.gl1Type == 2?(data.formal.fineAggregatePlan?data.formal.fineAggregatePlan+ "%" : ""):""),
            cell2: data.formal.gl2Type == 1 ?(data.formal.coarseAggregatePlan?data.formal.coarseAggregatePlan+ "%" : "") : (data.formal.gl2Type == 2?(data.formal.fineAggregatePlan?data.formal.fineAggregatePlan+ "%" : ""):""),
            cell3: data.formal.gl3Type == 1 ?(data.formal.coarseAggregatePlan?data.formal.coarseAggregatePlan+ "%" : "") : (data.formal.gl3Type == 2?(data.formal.fineAggregatePlan?data.formal.fineAggregatePlan+ "%" : ""):""),
            cell4: data.formal.gl4Type == 1 ?(data.formal.coarseAggregatePlan?data.formal.coarseAggregatePlan+ "%" : "") : (data.formal.gl4Type == 2?(data.formal.fineAggregatePlan?data.formal.fineAggregatePlan+ "%" : ""):""),
            cell5: data.formal.gl5Type == 1 ?(data.formal.coarseAggregatePlan?data.formal.coarseAggregatePlan+ "%" : "") : (data.formal.gl5Type == 2?(data.formal.fineAggregatePlan?data.formal.fineAggregatePlan+ "%" : ""):""),
            cell6: data.formal.gl6Type == 1 ?(data.formal.coarseAggregatePlan?data.formal.coarseAggregatePlan+ "%" : "") : (data.formal.gl6Type == 2?(data.formal.fineAggregatePlan?data.formal.fineAggregatePlan+ "%" : ""):""),
            cell7: data.formal.cementRange ? data.formal.cementRange + "%" : "",
            cell8: data.formal.waterRange ? data.formal.waterRange + "%" : "",
            cell9: "",
            cell10:"",
            cell11: "",
            cell12: ""
        })
        if (data.formal && data.warnInfo) {
            if (data.warnInfo.typeArray) {
                detailInfo.value.eachone.push({
                    cell1: data.warnInfo.typeArray.indexOf(data.formal.gl1Type == 1 ? 6 : (data.formal.gl1Type == 2 ? 7 : -1)) > -1 ? "-1" : "1",
                    cell2: data.warnInfo.typeArray.indexOf(data.formal.gl2Type == 1 ? 6 : (data.formal.gl2Type == 2 ? 7 : -1)) > -1 ? "-1" : "1",
                    cell3: data.warnInfo.typeArray.indexOf(data.formal.gl3Type == 1 ? 6 : (data.formal.gl3Type == 2 ? 7 : -1)) > -1 ? "-1" : "1",
                    cell4: data.warnInfo.typeArray.indexOf(data.formal.gl4Type == 1 ? 6 : (data.formal.gl4Type == 2 ? 7 : -1)) > -1 ? "-1" : "1",
                    cell5: data.warnInfo.typeArray.indexOf(data.formal.gl5Type == 1 ? 6 : (data.formal.gl5Type == 2 ? 7 : -1)) > -1 ? "-1" : "1",
                    cell6: data.warnInfo.typeArray.indexOf(data.formal.gl6Type == 1 ? 6 : (data.formal.gl6Type == 2 ? 7 : -1)) > -1 ? "-1" : "1",
                    cell7: data.warnInfo.typeArray.indexOf(8) > -1 ? "-1" : "1",
                    cell8: data.warnInfo.typeArray.indexOf(10) > -1 ? "-1" : "1",
                    cell9: "",
                    cell10: "",
                    cell11: "",
                    cell12: ""
                })
            } else {
                detailInfo.value.eachone.push({
                    cell1: "",
                    cell2: "",
                    cell3: "",
                    cell4: "",
                    cell5: "",
                    cell6: "",
                    cell7: "",
                    cell8: "",
                    cell9: "",
                    cell10: "",
                    cell11: "",
                    cell12: ""
                })
            }
        }
        else {
            detailInfo.value.eachone.push({
                cell1: "",
                cell2: "",
                cell3: "",
                cell4: "",
                cell5: "",
                cell6: "",
                cell7: "",
                cell8: "",
                cell9: "",
                cell10: "",
                cell11: "",
                cell12: ""
            })
        }

        if (queryinfo.value.type == 3) {
            detailInfo.value.warnInfo = data.warnInfo;
            if (detailInfo.value.warnInfo) detailInfo.value.warnInfo.describe = setDescribe(data.warnInfo.type);

            console.log(detailInfo.value)

            showModalInfo.value.isStableShow = true;
        } else {
            detailInfo.value.warnInfo = data.warnInfo;
            if (detailInfo.value.warnInfo) detailInfo.value.warnInfo.describe = setDescribe(data.warnInfo.type);

            console.log(detailInfo.value)

            showModalInfo.value.isShow = true;
        }
    }

    return {
        queryinfo,
        detailInfo,
        rawInfoList,
        showModalInfo,

        getRawInfo,
        getDetailInfo,
        getRawByLatery,
        getRawByLateryByMpId,
    }
}